package cn.com.kgc;



import java.util.concurrent.locks.Lock;
        import java.util.concurrent.locks.ReentrantLock;

/*银行有一个账户。有两个储户分别向同一个账户存3000元，每次存1000，存3次。每次存完打印账户余额。注意：效果要是交替打印！(多线程实现)*/
class  Save implements Runnable{
    int sum=0;
    int a = 0;
    int b =0;
    Lock loc = new ReentrantLock();
    static boolean bool=true;
    public  void run(){
        for(int i=1;i<=6;i++){
            synchronized(this){
                notify();//先唤醒另一个线程
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if(sum<6000){
                    sum+=1000;
                    if(Thread.currentThread().getName().equals("账户一")){
                        a+=1000;
                        System.out.println(Thread.currentThread().getName()+"目前存了"+a);
                    }else{
                        b+=1000;
                        System.out.println(Thread.currentThread().getName()+"目前存了"+b);
                    }

                    System.out.println("目前余额"+sum);
                    try {
                        wait();//打印完后等待下一次被唤醒
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }



    public static void main(String[] args) {
        Save c = new Save();
        Thread t1 = new Thread(c,"账户一");
        Thread t2 = new Thread(c,"账户二");
        t1.start();
        t2.start();
    }
}

